package view.bookAdmiView;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dao.BookDao;
import dao.BookTypeDao;
import model.Book;
import model.BookType;
import util.DbUtil;
import util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import javax.swing.ImageIcon;
import java.awt.Color;

public class BookTypeManageInterFrm extends JInternalFrame {
	private JTable bookTypeTable;
	private JTextArea bookTypeDescTxt;
	
	
	private DbUtil dbUtil=new DbUtil();
	private BookTypeDao bookTypeDao=new BookTypeDao();
	private BookDao bookDao=new BookDao();
	private JTextField s_bookTypeName;
	private JTextField idTxt;
	private JTextField bookTypeNameTxt;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookTypeManageInterFrm frame = new BookTypeManageInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookTypeManageInterFrm() {
		setBackground(new Color(224, 255, 255));
		setClosable(true);
		setIconifiable(true);
		setTitle("图书类别管理");
		setBounds(100, 100, 1013, 784);
		
		JScrollPane bookTypeScrollPane = new JScrollPane();
		
		JLabel s_bookTypeName_1 = new JLabel("图书类别名称：");
		s_bookTypeName_1.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_bookTypeName = new JTextField();
		s_bookTypeName.setFont(new Font("宋体", Font.BOLD, 25));
		s_bookTypeName.setColumns(10);
		
		JButton searchButton = new JButton("查询");
		searchButton.setIcon(new ImageIcon(BookTypeManageInterFrm.class.getResource("/images/bookAdmi/查询.png")));
		searchButton.setFont(new Font("宋体", Font.BOLD, 25));
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeSearchActionPerformed(e);
				
				
			}
		});
		
		JPanel bookTypePanel = new JPanel();
		bookTypePanel.setBackground(new Color(255, 255, 224));
		bookTypePanel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(56)
					.addComponent(s_bookTypeName_1)
					.addGap(48)
					.addComponent(s_bookTypeName, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE)
					.addGap(44)
					.addComponent(searchButton)
					.addContainerGap(498, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(34)
					.addComponent(bookTypeScrollPane, GroupLayout.DEFAULT_SIZE, 926, Short.MAX_VALUE)
					.addGap(37))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(26)
					.addComponent(bookTypePanel, GroupLayout.DEFAULT_SIZE, 926, Short.MAX_VALUE)
					.addGap(45))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(56)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(s_bookTypeName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(searchButton)
						.addComponent(s_bookTypeName_1))
					.addGap(34)
					.addComponent(bookTypeScrollPane, GroupLayout.PREFERRED_SIZE, 257, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
					.addComponent(bookTypePanel, GroupLayout.PREFERRED_SIZE, 283, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		
		JLabel bookTypeId = new JLabel("编号：");
		bookTypeId.setFont(new Font("宋体", Font.BOLD, 25));
		
		idTxt = new JTextField();
		idTxt.setFont(new Font("宋体", Font.BOLD, 25));
		idTxt.setEditable(false);
		idTxt.setColumns(10);
		
		JLabel bookTypeName = new JLabel("图书类别名称：");
		bookTypeName.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookTypeNameTxt = new JTextField();
		bookTypeNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		bookTypeNameTxt.setColumns(10);
		
		JLabel bookTypeDesc = new JLabel("描述：");
		bookTypeDesc.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookTypeDescTxt = new JTextArea();
		bookTypeDescTxt.setFont(new Font("宋体", Font.BOLD, 25));
		
		JButton updateButton = new JButton("修改");
		updateButton.setFont(new Font("宋体", Font.BOLD, 25));
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeUpdateActionEvent(e);
			}
		});
		
		JButton deleteButton = new JButton("删除");
		deleteButton.setIcon(new ImageIcon(BookTypeManageInterFrm.class.getResource("/images/bookAdmi/取消.png")));
		deleteButton.setFont(new Font("宋体", Font.BOLD, 25));
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeDeleteActionEvent(e);
			}
		});
		GroupLayout gl_bookTypePanel = new GroupLayout(bookTypePanel);
		gl_bookTypePanel.setHorizontalGroup(
			gl_bookTypePanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookTypePanel.createSequentialGroup()
					.addGroup(gl_bookTypePanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_bookTypePanel.createSequentialGroup()
							.addComponent(bookTypeId)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(54)
							.addComponent(bookTypeName)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(bookTypeNameTxt, GroupLayout.PREFERRED_SIZE, 311, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_bookTypePanel.createSequentialGroup()
							.addComponent(bookTypeDesc)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(bookTypeDescTxt, GroupLayout.DEFAULT_SIZE, 841, Short.MAX_VALUE)))
					.addContainerGap())
				.addGroup(Alignment.TRAILING, gl_bookTypePanel.createSequentialGroup()
					.addContainerGap(239, Short.MAX_VALUE)
					.addComponent(updateButton)
					.addGap(133)
					.addComponent(deleteButton)
					.addGap(328))
		);
		gl_bookTypePanel.setVerticalGroup(
			gl_bookTypePanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookTypePanel.createSequentialGroup()
					.addGroup(gl_bookTypePanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookTypeId)
						.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookTypeName)
						.addComponent(bookTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_bookTypePanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookTypeDesc)
						.addComponent(bookTypeDescTxt, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_bookTypePanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(deleteButton)
						.addComponent(updateButton))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		bookTypePanel.setLayout(gl_bookTypePanel);
		
		bookTypeTable = new JTable();
		bookTypeTable.setBackground(new Color(250, 235, 215));//设置表格背景颜色
		bookTypeTable.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,20));//设置表格开头字体
		bookTypeTable.getTableHeader().setBackground(new Color(127, 255, 212));//设置表格开头背景颜色
		bookTypeTable.setFont(new Font("楷体", Font.BOLD, 22));//设置表格内容字体
		bookTypeTable.setForeground(new Color(0,0,0));//设置表格内容字体颜色		
		bookTypeTable.setRowHeight(bookTypeTable.getRowHeight()+20);//设置表格内容的行高
		bookTypeTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				bookTypeTableMousePressed(e);
			}
		});
		bookTypeTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(118);
		bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(200);
		bookTypeScrollPane.setViewportView(bookTypeTable);
		getContentPane().setLayout(groupLayout);
		
		this.fillTable(new BookType());

		//设置JFrame居中显示
		this.setLocation(400,20);
				
		//设置文本域边框
		bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
	}
	
	/**
	 * 图书类别删除事件处理
	 * @param e
	 */
	private void bookTypeDeleteActionEvent(ActionEvent evt) {
		String id=idTxt.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
			return;
		}
		int n=JOptionPane.showConfirmDialog(null, "确定要删除该条记录吗？");
		if(n==0) {
			Connection con=null;
			try {
				con=dbUtil.getCon();			
				int deleteNum=bookTypeDao.delete(con, id);
				if(deleteNum==1) {
					JOptionPane.showMessageDialog(null, "删除成功!");
					this.resetValue();
					this.fillTable(new BookType());					
					bookDao.delete2(con, id);//删除了图书类别。也要将数据库中相应的图书删除
				}
				else {
					JOptionPane.showMessageDialog(null, "删除失败!");
				}
			}catch(Exception e) {
				e.printStackTrace();
			}finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 图书类别更新事件处理
	 * @param evt
	 */
	private void bookTypeUpdateActionEvent(ActionEvent evt) {
		String id=idTxt.getText();
		String bookTypeName=bookTypeNameTxt.getText();
		String bookTypeDesc=bookTypeDescTxt.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
			return;
		}
		if(StringUtil.isEmpty(bookTypeName)) {
			JOptionPane.showMessageDialog(null, "图书类别名称不能为空!");
			return;
		}
		BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int modifyNum=bookTypeDao.update(con, bookType);
			if(modifyNum==1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				this.resetValue();
				this.fillTable(new BookType());
				
			}else {
				JOptionPane.showMessageDialog(null, "修改失败");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "修改失败");
			}
		}
		
	}

	/**
	 * 表格行点击事件处理
	 * @param e
	 */
	private void bookTypeTableMousePressed(MouseEvent e) {
		int row =bookTypeTable.getSelectedRow();
		idTxt.setText((String)bookTypeTable.getValueAt(row, 0));
		bookTypeNameTxt.setText((String)bookTypeTable.getValueAt(row, 1));
		bookTypeDescTxt.setText((String)bookTypeTable.getValueAt(row, 2));
		
	}

	/**
	 * 图书类别搜索事件处理
	 * @param evt
	 */
	private void bookTypeSearchActionPerformed(ActionEvent evt) {
		String s_bookTypeName=this.s_bookTypeName.getText();
		BookType bookType=new BookType();
		bookType.setBookTypeName(s_bookTypeName);
		this.fillTable(bookType);
	}

	/**
	 * 初始化表格
	 * @param bookType
	 */
	private void fillTable(BookType bookType) {
		DefaultTableModel dtm=(DefaultTableModel) bookTypeTable.getModel();
		dtm.setRowCount(0);//设置成0行
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookTypeDao.list(con, bookType);
			while(rs.next()) {
				Vector v=new Vector();
				v.add(rs.getString("id"));
				v.add(rs.getString("bookTypeName"));
				v.add(rs.getString("bookTypeDesc"));
				dtm.addRow(v);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 重置表单
	 */
	private void resetValue() {
		this.idTxt.setText("");
		this.bookTypeNameTxt.setText("");
		this.bookTypeDescTxt.setText("");
	}
	
	
}
